package com.ydgk.servlet;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Book;
import com.ydgk.dao.BookDao;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "UpdateBookServlet", urlPatterns = "/UpdateBookServlet.action")
public class UpdateBookServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Map map = request.getParameterMap();
        Book book = new Book();
        try {
            BeanUtils.populate(book,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        BookDao bookDao = new BookDao();

        int i = bookDao.updateById(book);

        HashMap map2 = new HashMap();

        map2.put("flag" , i>0?true:false);

        response.getWriter().print(JSON.toJSONString(map2));
    }


}
